package com.eveinfo.display.skills;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.eveinfo.EveInfo;
import com.eveinfo.api.character.sheet.ApiSheet;
import com.eveinfo.api.utils.skilllist.ApiSkill;
import com.eveinfo.api.utils.skilllist.ApiSkillGroup;
import com.eveinfo.display.SheetView;
import com.eveinfo.display.planner.PlannerTools;
import com.eveinfo.tools.ActivityLauncher;

public class SkillsAdapter extends BaseAdapter {

	private final List<ApiSkillGroup> skills;
	private final Context context;
	private Integer[] unfold;
	private SkillsFilter filter;
	private final ApiSheet sheet;

	public SkillsAdapter(Activity activity, ListView listview, List<ApiSkillGroup> skills, ApiSheet sheet) {
		this.context = activity;
		this.skills = skills;
		this.filter = AllSkillsFilter.getInstance();
		this.sheet = sheet;
		final int size = skills.size();
		this.unfold = new Integer[size];
		for (int i = 0; i < size; i++) {
			unfold[i] = 0;
		}
		listview.setAdapter(this);
	}

	public Object getItem(int position) {
		return null;
	}

	public int getCount() {
		int total = 0;
		final List<ApiSkillGroup> skills = this.skills;
		int size = skills.size();
		for (int i = 0; i < size; i++) {
			if (unfold[i] == 1) {
				total++;
				ApiSkillGroup section = skills.get(i);
				for (ApiSkill skill : section.getSkills()) {
					if (filter.isDisplay(skill)) {
						total++;
					}
				}
			} else {
				total++;
			}
		}
		return total;
	}

	public boolean areAllItemsSelectable() {
		return false;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		int pos = position;

		final List<ApiSkillGroup> skills = this.skills;
		int skillsSize = skills.size();
		for (int i = 0; i < skillsSize; i++) {
			ApiSkillGroup section = skills.get(i);
			final int y = i;
			int size = 1;
			int realPos = pos - 1;
			ArrayList<ApiSkill> sectionSkills = section.getSkills();

			if (unfold[i] == 1) {
				int z = pos - 1;
				for (ApiSkill skill : sectionSkills) {
					if (filter.isDisplay(skill)) {
						z--;
						size++;
					} else {
						if (z >= 0) {
							realPos++;
						}
					}
				}
			}

			if (pos == 0) {
				TextView label = new TextView(context);
				label.setPadding(5, 3, 0, 3);
				label.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						if (unfold[y] == 0) {
							unfold[y] = 1;
						} else {
							unfold[y] = 0;
						}
						notifyDataSetChanged();
					}
				});
				int number = 0;
				for (ApiSkill skill : sectionSkills) {
					if (filter.isDisplay(skill)) {
						number++;
					}
				}
				label.setTextSize(20);
				label.setText(Html.fromHtml(section.getGroupName() + "<br/><small><small><small>  " + number
						+ " skills</small></small></small>"));
				return label;
			} else if (pos < size) {
				SkillsViewRenderer renderer = new SkillsViewRenderer(context);
				renderer.displaySkill(section.getSkillWithPosition(realPos), sheet);

				final int typeID = section.getSkillWithPosition(realPos).getTypeID();
				renderer.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						ActivityLauncher.displaySkill(context, typeID, sheet.getCharacterID());
					}
				});

				renderer.setOnLongClickListener(new OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						Dialog dialog = PlannerTools.createDialogAddSkill(context, SheetView.getPlannerManager(),
								sheet, EveInfo.getData().getSkills().getSkill(typeID));
						if (dialog == null) {
							AlertDialog.Builder builder = new AlertDialog.Builder(context);
							builder.setMessage("No planner");
							AlertDialog alert = builder.create();
							alert.show();
							return true;
						}
						dialog.show();
						return true;
					}
				});

				return renderer;
			}
			pos -= size;
		}

		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setFilter(SkillsFilter filter) {
		this.filter = filter;
		notifyDataSetChanged();
	}

}
